
 
package com.junitUtil.aspect;	

import com.junitUtil.spring.DecoratorBeanFactory;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;

import org.aspectj.weaver.loadtime.Aj;
import org.aspectj.weaver.loadtime.ClassPreProcessor;



/**
 * @see org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter
 * 重写静态语句中 s_preProcessor的赋值，使用自定义的WeavingContext
 * 
 **/

public class ClassPreProcessorAgentAdapterLocal implements ClassFileTransformer{
// extends ClassPreProcessorAgentAdapter {
    /**
     * Concrete preprocessor.
     */
    private static ClassPreProcessor s_preProcessor;

    static {
        try {
            s_preProcessor = new Aj(new WeavingContextLocal(DecoratorBeanFactory.class.getClassLoader()));
            s_preProcessor.initialize();
        } catch (Exception e) {
            throw new ExceptionInInitializerError("could not initialize JSR163 preprocessor due to: " + e.toString());
        }
    }
    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] bytes) throws IllegalClassFormatException {
        if (className.startsWith("org.aspectj") || className.startsWith("org/aspectj")||className.contains("springframework")||className.contains("aspectj/CuckooTaskAspect")) {
        	
            return bytes;
        }
        return s_preProcessor.preProcess(className, bytes, loader, protectionDomain);
    }

}

